﻿using System;
using System.Windows.Forms;

using Moe.ECamel.Common.Entities;
using Moe.ECamel.Client.Controllers;
using Moe.ECamel.Client.WinForms.Common;
using Moe.ECamel.Client.WinForms.Controls;
using Moe.ECamel.Common.Security;
using Moe.ECamel.Common.Utils;

namespace Moe.ECamel.Client.WinForms.Forms
{
	public partial class ECamelMainForm : ECamelForm
	{
		private const String DefaultCaption = "מערכת ספריות ארצית";
		private readonly SystemController _controller;

		public ECamelMainForm()
		{
			InitializeComponent();
		}
		public ECamelMainForm(SystemController controller)
			:this()
		{
			this._controller = controller;
			RefreshMenus();
			RefreshDate();
		}

		private void RefreshDate()
		{
			this.SystemDateToolStripSplitButton.Text = SystemContainer.Clock.Today.ToLongDateString();
		}

		private void RefreshPrimaryControl()
		{
			// Close current primary control(s)
			while (PrimaryControlPanel.Controls.Count > 0)
			{
				var control = PrimaryControlPanel.Controls[0];
				PrimaryControlPanel.Controls.RemoveAt(0);
				control.Dispose();
			}

			if (_controller.IsLoggedIn)
			{
				var primaryControl = CreatePrimaryControlByRole();
				PrimaryControlPanel.Controls.Add(primaryControl);
				primaryControl.Dock = DockStyle.Fill;

				String primaryRoleName = ECamelHelper.Roles[_controller.CurrentLoginInfo.PrimaryRole].RoleName;
				this.Text = DefaultCaption + " - " + _controller.CurrentLoginInfo.Username + " [" + primaryRoleName + "]";
			}
			else
			{
				this.Text = DefaultCaption;
			}

			RefreshMenus();
		}

		private Control CreatePrimaryControlByRole()
		{
			var loginInfo = _controller.CurrentLoginInfo;
			switch (loginInfo.PrimaryRole)
			{
				case Roles.Admin:
					return new LibraryTreeControl(new LibrariesController());
				case Roles.Manager:
				case Roles.Employee:
					var detailsController = new EntityDetailsController<Library>(DetailsMode.View, loginInfo.Library, new LibrariesController());
					return new LibraryControl(detailsController);
				case Roles.Customer:
					return new CustomerControl(loginInfo.Customer);
				default:
					throw new NotSupportedException("Unsupported primary role id: " + loginInfo.PrimaryRole);
			}
		}

		private void RefreshMenus()
		{
			LoginToolStripMenuItem.Enabled = !_controller.IsLoggedIn;
			LogoutToolStripMenuItem.Enabled = _controller.IsLoggedIn;
			ChangePasswordToolStripMenuItem.Enabled = _controller.IsLoggedIn;
		}

		private void LoginToolStripMenuItem_Click(object sender, EventArgs e)
		{
			using (var dialog = new ECamelLoginForm(_controller))
			{
				dialog.ShowDialog();
			}
			RefreshPrimaryControl();
		}
		private void LogoutToolStripMenuItem_Click(object sender, EventArgs e)
		{
			_controller.Logout();
			RefreshPrimaryControl();
		}
		private void ChangePasswordToolStripMenuItem_Click(object sender, EventArgs e)
		{
			using (var dialog = new ChangePasswordForm(_controller))
			{
				dialog.ShowDialog();
			}
		}
		private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void SystemDateToolStripSplitButton_ButtonClick(object sender, EventArgs e)
		{
			if (SystemContainer.Clock.Readonly)
				return;

			var newDate = DatePickerForm.ShowSelectSingleDateDialog(SystemContainer.Clock.Today, null, null);
			if (newDate != null)
			{
				SystemContainer.Clock.ChangeCurrentDateTime(newDate.Value);
				RefreshDate();
			}
		}
	}
}